package com.library.bookmanagement.controller;

import com.library.bookmanagement.entity.LoginUser;
import com.library.bookmanagement.entity.User;
import com.library.bookmanagement.service.AdminService;
import com.library.bookmanagement.service.LoginService;
import com.library.bookmanagement.service.UserService;
import com.library.bookmanagement.util.Constant;
import com.library.bookmanagement.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author ：ZXM
 * @date ：Created in 2020/5/16 9:02
 * @description：测试完成
 */
@CrossOrigin(origins = "*",maxAge = 3600)
@Controller
@ResponseBody
@RequestMapping("/login")
@Api(value = "登录Controller",tags = {"用户登录统一接口"})
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserDetailsService detailsService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    /**
    * @Description:确认注册接口
    * @Param: [loginUser]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @ApiOperation(value = "注册接口")
    @PostMapping("/register")
    public ResultUtil registerUser(@RequestBody LoginUser loginUser){
        User user = new User();
        user.setuName(loginUser.getUsername());
        user.setuImg("headImg.jpg");
        user.setuPassword(loginUser.getPassword());
        user.setuEmail(loginUser.getEmail());
        return adminService.addUser(user);
    }

    /**
    * @Description: 返回邮箱验证码
    * @Param: [email]
    * @return: com.library.bookmanagement.util.ResultUtil<java.lang.String>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @ApiOperation(value = "返回用户注册时邮箱验证码,role=1w为用户注册，role=2为管理员注册")
    @GetMapping("/getVertifyCode")
    public ResultUtil<String> getVertifyCode(
            @ApiParam(value = "用户注册邮箱",name = "email")
            @RequestParam(name = "email") String email
    ){
        return loginService.getVertifyCode(email);
    }

}
